#!/usr/bin/env python3
# -*-coding:utf-8-*-

from PySide2.QtWidgets import QDialog, QLabel, QLineEdit, QCheckBox, \
    QPushButton, QHBoxLayout, QVBoxLayout, QApplication
from PySide2.QtCore import Qt, Signal, Slot


class FindDialog(QDialog):
    findNext = Signal(str, Qt.CaseSensitivity)
    findPrevious = Signal(str, Qt.CaseSensitivity)

    def __init__(self, parent=None):
        super().__init__(parent)
        label = QLabel("Find &what:")
        self.lineEdit = QLineEdit()
        label.setBuddy(self.lineEdit)

        self.caseCheckBox = QCheckBox("Match &case")
        self.backwardCheckBox = QCheckBox("Search &backward")
        self.findButton = QPushButton("&Find")
        self.findButton.setDefault(True)
        self.findButton.setEnabled(False)
        closeButton = QPushButton("Close")

        self.lineEdit.textChanged.connect(self.enableFindButton)
        self.findButton.clicked.connect(self.findClicked)
        closeButton.clicked.connect(self.close)

        topLeftLayout = QHBoxLayout()
        topLeftLayout.addWidget(label)
        topLeftLayout.addWidget(self.lineEdit)
        leftLayout = QVBoxLayout()
        leftLayout.addLayout(topLeftLayout)
        leftLayout.addWidget(self.caseCheckBox)
        leftLayout.addWidget(self.backwardCheckBox)
        rightLayout = QVBoxLayout()
        rightLayout.addWidget(self.findButton)
        rightLayout.addWidget(closeButton)
        rightLayout.addStretch()
        mainLayout = QHBoxLayout()
        mainLayout.addLayout(leftLayout)
        mainLayout.addLayout(rightLayout)
        self.setLayout(mainLayout)

        self.setWindowTitle("Find")
        self.setFixedHeight(self.sizeHint().height())

    def enableFindButton(self, text):
        self.findButton.setEnabled(bool(text))

    @Slot()
    def findClicked(self):
        text = self.lineEdit.text()
        if self.caseCheckBox.isChecked():
            cs = Qt.CaseSensitive
        else:
            cs = Qt.CaseInsensitive

        if self.backwardCheckBox.isChecked():
            self.findPrevious.emit(text, cs)
        else:
            self.findNext.emit(text, cs)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    findDialog = FindDialog()


    def find(text, cs):
        print('find:', text, 'cs', cs)


    def findp(text, cs):
        print('findp:', text, 'cs', cs)


    findDialog.findNext.connect(find)
    findDialog.findPrevious.connect(findp)
    findDialog.show()
    sys.exit(app.exec_())
